namespace Sustainalytics.Portfolios.BL.Tests.IntegrationTest
{
    using System;
    using System.Collections.Generic;
    using Sustainalytics.ClientDW.Entities;
    using Sustainalytics.Companies;
    using Sustainalytics.Controversies.BusinessLogic;
    using Sustainalytics.DataLayer.Shared;
    using Sustainalytics.Entities;
    using Sustainalytics.Portfolios.BL.Model;
    using Sustainalytics.Utils;

    using Xunit;

    public class SystemPortfolioManagementTests
    {
        private readonly string _localDatabase = "mongodb://localhost:27017/ClientPlatform";

        private readonly string _localDatabaseDW = "mongodb://localhost:27017/ClientDW_B";

        private readonly IRepositoryCollection<Portfolio> _portfolioRepository;

        private readonly string _stageDatabase = "mongodb://admin:G4nd4lf2014@54.171.12.91:27017/ClientDW_A";

        private readonly ISystemPortfolioManagement _sut;

        private readonly PortfolioUserClaims userClaimsFake = new PortfolioUserClaims
        {
            UserName =
                                                                          "teodor.bajan@sustainalytics.com",
            UserFullName = "Teodor Bajan",
            UserId =
                                                                          new Guid(
                                                                          "4387c2a7-1eda-11e4-92ce-6c3be5be9f88"),
            AccountId =
                                                                          new Guid(
                                                                          "528ea769-2d17-e411-92ce-6c3be5be9f98")
        };

        private readonly Dictionary<string, string> _serviceBusParams = new Dictionary<string, string>
            {
                { "ConnectionString", "Endpoint=sb://sust-test-ns.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=Dh/4Cq0qrOC7xOJFUYHLGJvQ6pqj4BuwjTS4HUjL6uQ=" },
                { "RegionEndpoint", "TT" },
                { "SessionId", "Portfolios" },
                { "TopicPath", "test-topic" },
            };

        public SystemPortfolioManagementTests()
        {
            this._portfolioRepository = new RepositoryCollection<Portfolio>(this._localDatabase);

            this._companyDataRepository =
                new PlainMongoBl<CorporateData>(new RepositoryCollection<CorporateData>(this._stageDatabase));
            this._sut =
                new SystemPortfolioManagement(
                    new CompanyMatchingLogic(MongoDB.Extensions.MongoFactory.GetCollectionFromDatabase<CorporateData>(this._stageDatabase)),
                    new PortfolioMatching(
                       new CompanyMatchingLogic(MongoDB.Extensions.MongoFactory.GetCollectionFromDatabase<CorporateData>(this._stageDatabase)), 
                       new CorporateDataUniversesLoader(MongoDB.Extensions.MongoFactory.GetCollectionFromDatabase<CorporateData>(this._stageDatabase))),
                    new RepositoryCollection<Portfolio>(this._localDatabaseDW),
                    new RepositoryCollection<PortfolioDetails>(this._localDatabaseDW));
        }

        private IPlainBl<CorporateData> _companyDataRepository { get; set; }

        [Fact]
        public void ShouldInsertAnAotomaticSystemPortfolioInDb()
        {
            this._sut.Add(
                new[]
                    {
                        new SystemPortfolioInfo()
                            {
                                Id = Guid.NewGuid(),
                                AccountIds = new[] { this.userClaimsFake.AccountId },
                                CompanyIds =
                                    new[] { new Guid("c4621c00-af02-4ec6-8399-af1489c9dfc9") },
                                Name = "SystemPortfolioTest"
                            }
                    });
        }

        [Fact]
        public void ShouldReadAllCustomPortfolios()
        {
            var portfolios = this._portfolioRepository.ReadAll();

            Assert.True(!portfolios.IsNullOrEmpty());
        }

        [Fact]
        public void ShouldReadAllSystemPortfolios()
        {
            var portfolios = new RepositoryCollection<Portfolio>(this._localDatabaseDW).ReadAll();

            Assert.False(portfolios.IsNullOrEmpty());
        }



        public class ManualSystemPortfolioTest : SystemPortfolioManagementTests
        {
            private new readonly IManualSystemPortfolioUpdatesHandler _sut;

            public ManualSystemPortfolioTest()
            {
                this._sut =
                    new ManualSystemPortfolioUpdatesHandler(
                         new PortfolioTypeCollection<Portfolio>(new RepositoryCollection<Portfolio>(_localDatabase), new RepositoryCollection<Portfolio>(this._localDatabaseDW)),
                        null,
                        null
                        );
            }

            [Fact(Skip = "Integration test")]

            public void ShouldAddAManualSystemPortfolioInDb()
            {
                var automaticPortfolio = new SystemPortfolioInfo()
                {
                    Id = Guid.NewGuid(),
                    AccountIds =
                                                     new[] { this.userClaimsFake.AccountId },
                    CompanyIds =
                                                     new[]
                                                         {
                                                             new Guid(
                                                                 "c4621c00-af02-4ec6-8399-af1489c9dfc9")
                                                         },
                    Name = "SystemPortfolioTest"
                };
                base._sut.Add(new[] { automaticPortfolio });
                var result = this._sut.Add("manualSystem", new[] { automaticPortfolio.Id }, this.userClaimsFake);
                this._sut.Add(
                    "manual system 2",
                    new[] { automaticPortfolio.Id },
                    new PortfolioUserClaims
                    {
                        UserName = "teodor.bajan@sustainalytics.com",
                        UserFullName = "Teodor Bajan",
                        UserId = new Guid("4387c2a7-1eda-11e4-92ce-6c3be5be9f88"),
                        AccountId = new Guid("528ea769-2d17-e411-92ce-6c3be5be9f98")
                    });
                Assert.True(result.Id != default(Guid));
            }
        }
    }
}